import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:dart_mock/dart_mock.dart' as mock;
import 'package:qianyi/network/http_entity.dart';
import 'package:qianyi/network/http_manager.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../entity/mine/User.dart';
import '../network/http_config.dart';

class TokenController extends GetxController {
  static TokenController get to => Get.find();
  TextEditingController usernameTextController = TextEditingController();
  TextEditingController  passwordTextController = TextEditingController();
  String userName = "";
  String password = "";
  String tokenString = "";
  String uid = "";
  RxString avatar = "".obs;
  RxString cover = "".obs;

  RxMap<String,User> userMap = RxMap<String,User>();
  RxList<User> user = RxList();
  TokenController() {
    initToken();
    getUid();
    getUserInfo();
  }
  Future<String> getUid() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();

    Map<String,dynamic> map = jsonDecode(prefs.get("userInfo") as String);
    uid = map['id'];
    return map['id'];
  }
   getUserInfo() async {

    SharedPreferences prefs = await SharedPreferences.getInstance();

    Map<String,dynamic> map = jsonDecode(prefs.get("userInfo") as String);
    user.add(User.fromJson(map));
    uid = map['id'];
    getAvatar();
}
    Future<String> getAvatar() async {
      String preAvatar = this.user!.value[0].avatar!;
      String preCover = this.user!.value[0].cover!;
      Map<String,String> params = {
        "path": preAvatar
      };
      HttpEntity httpEntity = await HttpManager.getInstance().request(url: '/util/fileoss/getFileUrl',params: params,method: Method.get) as HttpEntity;
      Map<String,String> params1 = {
        "path": preCover
      };
      HttpEntity httpEntity1 = await HttpManager.getInstance().request(url: '/util/fileoss/getFileUrl',params: params1,method: Method.get) as HttpEntity;

      this.avatar.value = httpEntity.getOriData()['data'];
      this.cover.value = httpEntity1.getOriData()['data'];
      return avatar.value;
    }
   void initToken()async {
     SharedPreferences prefs = await SharedPreferences.getInstance();
     this.tokenString = prefs.get("Jwt_token") as String;
   }
  Future setLocal(Map<String,dynamic> token) async {

    this.tokenString = token['Jwt_token'];
    String userInfo = jsonEncode(token["userInfo"]);
    SharedPreferences prefs = await SharedPreferences.getInstance();

    await prefs.setString("Jwt_token", tokenString);
    await prefs.setString("userInfo", userInfo);

  }
  /// 设置token
  void set() {

  }

  /// 删除token
  void delete() {

  }


    /// 清除用户信息
    Future<void> clear() async {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      await prefs.remove("Jwt_token");
      await prefs.remove("userInfo");
      user.clear();
      tokenString = "";
      uid = "";
      update(); // 通知依赖此控制器的Widget更新
      Get.toNamed("/login/password");
  }

  Future<void> updateUser() async {


    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.remove("userInfo");

    Map<String,String> params = {
      "uid":this.uid
    };
    HttpEntity httpEntity = await HttpManager.getInstance().request(url: "/platform/user/getUserInfo",method: Method.get,params: params) as HttpEntity;
    String userInfo = jsonEncode(httpEntity.getOriData()['data']);
    await prefs.setString("userInfo", userInfo);
    Map<String,dynamic> map = httpEntity.getOriData();
    user.add(User.fromJson(map['data']));

    getAvatar();
  }
}
